﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using NGuestBook.IDAL;
using NGuestBook.Entity;

namespace NGuestBook.AccessDAL
{
    public class AdminDAL : IAdminDAL
    {
        /**//// <summary>
        /// 插入管理员
        /// </summary>
        /// <param name="admin">管理员实体类</param>
        /// <returns>是否成功</returns>
        public bool Insert(AdminInfo admin)
        {
            string SQLCommand = "insert into [TAdmin]([Name],[Password]) values(@name,@password)";
            OleDbParameter[] parameters ={
                new OleDbParameter("name",OleDbType.VarChar,20),
                new OleDbParameter("password",OleDbType.VarChar,50)
            };
            parameters[0].Value = admin.Name;
            parameters[1].Value = admin.Password;

            return AccessDALHelper.OperateNonData(SQLCommand, parameters);
        }

        /**//// <summary>
        /// 删除管理员
        /// </summary>
        /// <param name="id">欲删除的管理员的ID</param>
        /// <returns>是否成功</returns>
        public bool Delete(int id)
        {
            string SQLCommand = "delete from [TAdmin] where [ID]=@id";
            OleDbParameter[] parameters ={
                new OleDbParameter("id",OleDbType.Integer)
            };
            parameters[0].Value = id;

            return AccessDALHelper.OperateNonData(SQLCommand, parameters);
        }

        /**//// <summary>
        /// 更新管理员信息
        /// </summary>
        /// <param name="admin">管理员实体类</param>
        /// <returns>是否成功</returns>
        public bool Update(AdminInfo admin)
        {
            string SQLCommand = "update [TAdmin] set [Name]=@name,[Password]=@password where [ID]=@id";
            OleDbParameter[] parameters ={
                new OleDbParameter("id",OleDbType.Integer),
                new OleDbParameter("name",OleDbType.VarChar,20),
                new OleDbParameter("password",OleDbType.VarChar,50)
            };
            parameters[0].Value = admin.ID;
            parameters[1].Value = admin.Name;
            parameters[2].Value = admin.Password;

            return AccessDALHelper.OperateNonData(SQLCommand, parameters);
        }

        /**//// <summary>
        /// 按ID取得管理员信息
        /// </summary>
        /// <param name="id">管理员ID</param>
        /// <returns>管理员实体类</returns>
        public AdminInfo GetByID(int id)
        {
            string SQLCommand = "select * from [TAdmin] where [ID]=@id";
            OleDbParameter[] parameters ={
                new OleDbParameter("id",OleDbType.Integer)
            };
            parameters[0].Value = id;

            return AccessDALHelper.OperateEntity<AdminInfo>(SQLCommand, parameters, AdminDataReaderToEntityStrategy.GetInstance());
        }

        /**//// <summary>
        /// 按用户名及密码取得管理员信息
        /// </summary>
        /// <param name="name">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>管理员实体类，不存在时返回null</returns>
        public AdminInfo GetByNameAndPassword(string name, string password)
        {
            string SQLCommand = "select * from [TAdmin] where [Name]=@name and [Password]=@password";
            OleDbParameter[] parameters ={
                new OleDbParameter("name",OleDbType.VarChar,20),
                new OleDbParameter("password",OleDbType.VarChar,50)
            };
            parameters[0].Value = name;
            parameters[1].Value = password;

            return AccessDALHelper.OperateEntity<AdminInfo>(SQLCommand, parameters, AdminDataReaderToEntityStrategy.GetInstance());
        }

        /**//// <summary>
        /// 按管理员名取得管理员信息
        /// </summary>
        /// <param name="name">管理员名</param>
        /// <returns>管理员实体类</returns>
        public AdminInfo GetByName(string name)
        {
            string SQLCommand = "select * from [TAdmin] where [Name]=@name";
            OleDbParameter[] parameters ={
                new OleDbParameter("name",OleDbType.VarChar,20)
            };
            parameters[0].Value = name;

            return AccessDALHelper.OperateEntity<AdminInfo>(SQLCommand, parameters, AdminDataReaderToEntityStrategy.GetInstance());
        }

        /**//// <summary>
        /// 取得全部管理员信息
        /// </summary>
        /// <returns>管理员实体类集合</returns>
        public IList<AdminInfo> GetAll()
        {
            string SQLCommand = "select * from [TAdmin]";

            return AccessDALHelper.OperateEntityCollection<AdminInfo>(SQLCommand, null, AdminDataReaderToEntityStrategy.GetInstance());
        }
    }
}